package cn.edu.zafu.bigdata.x.zyy;

import java.io.*;
import java.util.*;

public class StudentInfoSystem {
    private static final String FILE_NAME = "students.dat";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Map<String, Student> students = loadStudents();

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent(scanner, students);
                    break;
                case 2:
                    findStudent(scanner, students);
                    break;
                case 3:
                    updateStudent(scanner, students);
                    break;
                case 4:
                    deleteStudent(scanner, students);
                    break;
                case 5:
                    saveStudents(students);
                    System.out.println("程序已退出。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private static void addStudent(Scanner scanner, Map<String, Student> students) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (students.containsKey(id)) {
            System.out.println("学号重复，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.put(id, student);
        saveStudents(students);
        System.out.println("学生信息添加成功。");
    }

    private static void findStudent(Scanner scanner, Map<String, Student> students) {
        System.out.print("请输入学号 (输入'QUIT'退出): ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (students.containsKey(id)) {
                System.out.println(students.get(id));
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            id = scanner.nextLine();
        }
    }

    private static void updateStudent(Scanner scanner, Map<String, Student> students) {
        System.out.print("请输入学号 (输入'QUIT'退出): ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (students.containsKey(id)) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                students.get(id).setName(name);
                students.get(id).setPhone(phone);
                saveStudents(students);
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            id = scanner.nextLine();
        }
    }

    private static void deleteStudent(Scanner scanner, Map<String, Student> students) {
        System.out.print("请输入学号 (输入'QUIT'退出): ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (students.containsKey(id)) {
                students.remove(id);
                saveStudents(students);
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            id = scanner.nextLine();
        }
    }

    private static Map<String, Student> loadStudents() {
        Map<String, Student> students = new HashMap<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (Map<String, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载学生信息失败，使用空数据。");
        }
        return students;
    }
    private static void saveStudents(Map<String, Student> students) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("保存学生信息失败。");
        }
    }

    static class Student implements Serializable {
        private String id;
        private String name;
        private String phone;

        public Student(String id, String name, String phone) {
            this.id = id;
            this.name = name;
            this.phone = phone;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", phone='" + phone + '\'' +
                    '}';
        }
    }
}
